//
//  SchoolHeadView.m
//  Arts1
//
//  Created by lanou on 15/8/28.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "SchoolHeadView.h"
#import "AllHeader.h"
#import "HeaderCollectionViewCell.h"



@implementation SchoolHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        _mainCollectionview = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _mainCollectionview.backgroundColor = [UIColor whiteColor];
        _mainCollectionview.delegate = self;
        _mainCollectionview.dataSource = self;
        _mainCollectionview.showsHorizontalScrollIndicator = NO;
        [_mainCollectionview registerClass:[HeaderCollectionViewCell class] forCellWithReuseIdentifier:@"headercell"];

        //  添加阴影
        [UIView viewWideForShadowColor:self];
        
        [self addSubview:_mainCollectionview];
        _count = 0;
     //   [self addTimer];
        
    }
    return self;
}

- (void)addTimer{
    _timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(changeCOontentOffset) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}

- (void)removeTimer{
    [_timer invalidate];
}


- (void)changeCOontentOffset{
    if (_count % 2 == 0) {
        _mainCollectionview.contentOffset = CGPointMake(0, 0);
    } else {
        _mainCollectionview.contentOffset = CGPointMake(SCREEN_WIDTH - 10, 0);
    }
    _count ++;
}

- (void)setDataArr:(NSMutableArray *)dataArr{
    _dataArr = dataArr;
    [_mainCollectionview reloadData];
}


#pragma mark -- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView{


}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
 //   [self addTimer];
}



#pragma mark -- UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH - 5 * 10) / 4, (SCREEN_WIDTH - 5 * 10) / 4);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"headercell" forIndexPath:indexPath];
    AreaModel *area = _dataArr[indexPath.row];
    cell.area = area;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 获取点击的cell
    HeaderCollectionViewCell *cell = (HeaderCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (_delegate && [_delegate respondsToSelector:@selector(changeSchoolByArea:)]) {
       [_delegate changeSchoolByArea:cell.area];
    }

}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
